<template>
  <div class="father-box">
    <el-row>
      <el-col :xs="20" :sm="12" :md="12" :lg="12" :xl="12">
        <el-form>
          <el-input placeholder="请输入用户名" v-model="formData.username"></el-input>
          <el-input placeholder="请输入密码" v-model="formData.password" show-password></el-input>
          <el-button style="width: 100%" type="primary" @click="onSubmit">登录</el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {login} from '@/api'

export default {
  data() {
    return {
      formData: {
        username: '',
        password: '',
      }
    }
  },
  methods: {
    // 提交登录
    onSubmit() {
      login(this.formData).then((res) => {
        if (res.data.flag) {
          this.$message.success(res.data.message)
          // 将令牌转到localStorage中
          localStorage.setItem('token', res.data.data)
          // 跳转到主页
          this.$router.push({path: '/'})
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
}
</script>

<style lang="less">
.father-box {
  height: 80vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>